package com.lgmshare.component.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import androidx.appcompat.widget.Toolbar;

import com.lgmshare.component.R;

public class TitleCenterToolbar extends Toolbar {

    public RelativeLayout mRelativeLayout;
    public TextView mTitleTextView;
    public ImageView mTitleImageView;
    private boolean mTitleCenter = true;

    public TitleCenterToolbar(Context context) {
        super(context);
        init();
    }

    public TitleCenterToolbar(Context context, AttributeSet arr) {
        super(context, arr);
        init();
        TypedArray typedArray = context.obtainStyledAttributes(arr, R.styleable.Toolbar, 0, 0);
        int titleTextAppearance = typedArray.getResourceId(R.styleable.Toolbar_titleTextAppearance, 0);
        int subtitleTextAppearance = typedArray.getResourceId(R.styleable.Toolbar_subtitleTextAppearance, 0);
        mTitleTextView.setTextAppearance(context, titleTextAppearance);
        setTitleTextAppearance(context, titleTextAppearance);
        setSubtitleTextAppearance(context, subtitleTextAppearance);
        typedArray.recycle();
    }

    private void init() {
        mRelativeLayout = new RelativeLayout(getContext());
        LayoutParams tLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        tLayoutParams.gravity = Gravity.CENTER;

        mTitleTextView = new TextView(getContext());
        mTitleTextView.setId(R.id.toolbar_center_title);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        mTitleTextView.setSingleLine();
        mTitleTextView.setEllipsize(TruncateAt.MIDDLE);
        mRelativeLayout.addView(mTitleTextView, layoutParams);

        layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        mTitleImageView = new ImageView(getContext());
        mTitleImageView.setId(R.id.toolbar_center_image_title);
        mTitleImageView.setVisibility(GONE);
        mTitleImageView.setScaleType(ScaleType.CENTER_INSIDE);
        mRelativeLayout.addView(mTitleImageView, layoutParams);

        addView(mRelativeLayout, tLayoutParams);
    }

    public void setTitleCenter(boolean center) {
        String title = "";
        if (getTitle() != null) {
            title = getTitle().toString();
        }
        mTitleCenter = center;
        if (TextUtils.isEmpty(title)) {
            if (getTitle() != null) {
                title = getTitle().toString();
            }
        }
        setTitle(title);
    }

    @Override
    public void setTitle(@StringRes int resId) {
        String title = getContext().getString(resId);
        setTitle(title);
    }

    @Override
    public void setTitle(CharSequence title) {
        if (mTitleCenter) {
            super.setTitle("");
            mTitleTextView.setText(title);
        }else{
            super.setTitle(title);
            mTitleTextView.setText("");
        }
    }

    @Override
    public CharSequence getTitle() {
        if (mTitleCenter) {
            return mTitleTextView.getText();
        }
        return super.getTitle();
    }

    @Override
    public void setTitleTextColor(@ColorInt int resId) {
        super.setTitleTextColor(resId);
        mTitleTextView.setTextColor(resId);
    }

    @Override
    public void setTitleTextAppearance(Context context, @StyleRes int resId) {
        super.setTitleTextAppearance(context, resId);
        mTitleTextView.setTextAppearance(context, resId);
    }

    public void setImageTitle(@DrawableRes int iconRes) {
        setTitle("");
        mTitleImageView.setImageResource(iconRes);
        mTitleImageView.setVisibility(VISIBLE);
    }
}